package com.andy.ui.view

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.andy.ui.R
import com.andy.ui.ext.dp2px
import com.andy.ui.ext.gone
import com.andy.ui.ext.visible

/**
 * Created by luofan on 2018/6/22.
 * 输入框扩展
 */
class AndyInputLayout : FrameLayout {

    private var orientation = VERTICAL_CHOOSE

    private lateinit var mContext: Context

    private lateinit var tvTitle: TextView

    private lateinit var contentLayout: LinearLayout

    private lateinit var tvContent: TextView

    private lateinit var tvUnit: TextView

    private lateinit var tvError: TextView

    private lateinit var dividerView: View

    private lateinit var imgNextArrow: ImageView

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initLayout(context, attrs)
    }

    private fun initLayout(context: Context, attrs: AttributeSet?) {
        mContext = context
        val array = context.obtainStyledAttributes(attrs, R.styleable.AndyInputLayout)
        initView(array)

        val title = array.getString(R.styleable.AndyInputLayout_inputTitle)
        val titleSize = array.getDimensionPixelSize(R.styleable.AndyInputLayout_inputTitleSize, 14)
        val titleColor = array.getColor(R.styleable.AndyInputLayout_inputTitleColor, Color.BLACK)
        tvTitle.text = title
        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, titleSize.toFloat())
        tvTitle.setTextColor(titleColor)

        val content = array.getString(R.styleable.AndyInputLayout_inputContent)
        val contentSize = array.getDimensionPixelSize(R.styleable.AndyInputLayout_inputContentSize, 16)
        val contentColor = array.getColor(R.styleable.AndyInputLayout_inputContentColor, Color.BLACK)
        tvContent.text = content
        tvContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, contentSize.toFloat())
        tvContent.setTextColor(contentColor)

        val unit = array.getString(R.styleable.AndyInputLayout_inputUnit)
        val unitSize = array.getDimensionPixelSize(R.styleable.AndyInputLayout_inputUnitSize, 16)
        val unitColor = array.getColor(R.styleable.AndyInputLayout_inputUnitColor, Color.BLACK)
        tvUnit.text = unit
        tvUnit.setTextSize(TypedValue.COMPLEX_UNIT_SP, unitSize.toFloat())
        tvUnit.setTextColor(unitColor)

        val error = array.getString(R.styleable.AndyInputLayout_inputError)
        val errorSize = array.getDimensionPixelSize(R.styleable.AndyInputLayout_inputErrorSize, 16)
        val errorColor = array.getColor(R.styleable.AndyInputLayout_inputErrorColor, Color.BLACK)
        tvError.text = error
        tvError.setTextSize(TypedValue.COMPLEX_UNIT_SP, errorSize.toFloat())
        tvError.setTextColor(errorColor)

        val arrowImgRes = array.getResourceId(R.styleable.AndyInputLayout_inputArrowImg, R.mipmap.arrow_next)
        imgNextArrow.setImageResource(arrowImgRes)

        val dividerHorizontalMargin = array.getDimensionPixelSize(R.styleable.AndyInputLayout_inputDividerHorizontalMargin, 0)
        val dividerColor = array.getColor(R.styleable.AndyInputLayout_inputDividerColor, Color.BLACK)
        val dividerHeight = array.getDimensionPixelSize(R.styleable.AndyInputLayout_inputDividerHeight, 1)
        val dividerParams = dividerView.layoutParams as LinearLayout.LayoutParams
        dividerParams.height = dividerHeight
        initHorizontalMargin(dividerParams, dividerHorizontalMargin)
        dividerView.setBackgroundColor(dividerColor)

        val horizontalMargin = array.getDimensionPixelSize(R.styleable.AndyInputLayout_inputHorizontalMargin, dp2px(16))
        val titleMarginTop = array.getDimensionPixelSize(R.styleable.AndyInputLayout_inputTitleMarginTop, dp2px(16))
        val contentMarginTop = array.getDimensionPixelSize(R.styleable.AndyInputLayout_inputContentMarginTop, 0)
        val contentHeight = array.getDimensionPixelSize(R.styleable.AndyInputLayout_inputContentHeight, dp2px(44))

        if (orientation == LinearLayout.VERTICAL) {
            val titleParams = tvTitle.layoutParams as LinearLayout.LayoutParams
            titleParams.topMargin = titleMarginTop
            initHorizontalMargin(titleParams, horizontalMargin)
        }

        val contentParams = contentLayout.layoutParams as LinearLayout.LayoutParams
        contentParams.topMargin = contentMarginTop
        contentParams.height = contentHeight
        initHorizontalMargin(contentParams, horizontalMargin)

        val errorParams = tvError.layoutParams as LinearLayout.LayoutParams
        initHorizontalMargin(errorParams, horizontalMargin)

        array.recycle()
    }

    private fun initHorizontalMargin(horizontalParams: LinearLayout.LayoutParams, margin: Int) {
        horizontalParams.marginStart = margin
        horizontalParams.marginEnd = margin
    }

    private fun initView(typeArray: TypedArray) {
        orientation = typeArray.getInt(R.styleable.AndyInputLayout_inputOrientation, VERTICAL_CHOOSE)
        when (orientation) {
            HORIZONTAL_CHOOSE -> {
                LayoutInflater.from(mContext).inflate(R.layout.layout_andy_choose_horizontal, this)
            }
            VERTICAL_CHOOSE -> {
                LayoutInflater.from(mContext).inflate(R.layout.layout_andy_choose_vertical, this)
            }
            HORIZONTAL_INPUT -> {
                LayoutInflater.from(mContext).inflate(R.layout.layout_andy_input_horizontal, this)
            }
            VERTICAL_INPUT -> {
                LayoutInflater.from(mContext).inflate(R.layout.layout_andy_input_vertical, this)
            }
        }
        contentLayout = findViewById(R.id.layout_content)
        tvTitle = findViewById(R.id.tv_title)
        tvContent = findViewById(R.id.tv_content)
        tvUnit = findViewById(R.id.tv_unit)
        tvError = findViewById(R.id.tv_error)
        dividerView = findViewById(R.id.view_divider)
        imgNextArrow = findViewById(R.id.img_next_arrow)
    }

    fun setError(errorTip: String) {
        tvError.text = errorTip
        tvError.visible()
    }

    fun setError(errorResId: Int) {
        tvError.text = mContext.getString(errorResId)
        tvError.visible()
    }

    fun hideError() {
        tvError.text = ""
        tvError.gone()
    }

    companion object {
        //垂直布局-选择
        const val HORIZONTAL_CHOOSE = 0

        //横向布局-选择
        const val VERTICAL_CHOOSE = 1

        //横向布局-输入
        const val HORIZONTAL_INPUT = 2

        //垂直布局-输入
        const val VERTICAL_INPUT = 3
    }

}